package cn.jinjimi.app.main.fragment;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.jinjimi.app.R;
import cn.jinjimi.app.entity.LinkBean;
import cn.jinjimi.app.interfaces.IGridLayoutAdapter;
import cn.jinjimi.app.main.extend.BaseFragment;
import cn.jinjimi.app.main.widget.EntranceView;
import cn.jinjimi.app.main.widget.FrescoImageView.FrescoImageView;
import cn.jinjimi.app.main.widget.XRecyclerView.XRecyclerView;
import cn.jinjimi.base.util.ViewUtils;
import cn.jinjimi.domain.entity.ProductCategory;
import cn.jinjimi.framework.router.RouterHelper;

/**
 * @author zhenhua
 * @date 2018/5/12
 */

public class ProductCategoryFragment extends BaseFragment {

    private XRecyclerView mRecycleview;
    private ProductCategory mProductCategory;
    private ProductCategoryAdapter mAdapter;

    public ProductCategoryFragment() {
        super();
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.fragment_product_category;
    }

    @Override
    protected void initial() {
        mRecycleview = findView().findViewById(R.id.recyclerview);
        mRecycleview.setPullRefreshEnabled(false);
        mRecycleview.addHeaderView(getHeaderView());
        mRecycleview.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        mAdapter = new ProductCategoryAdapter();
        mRecycleview.setAdapter(mAdapter);
        if (mProductCategory != null) {
            mAdapter.setData(mProductCategory.subs);
        }
    }

    private View getHeaderView() {
        EntranceView v = new EntranceView(getActivity());
        v.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
            ViewUtils.dip2px(getActivity(), 64)));
        v.setPadding(20, 20, 20, 20);
        if (mProductCategory != null) {
            FrescoImageView iv = v.findViewById(R.id.entrance_img);
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            iv.loadView(mProductCategory.img);
        }
        return v;
    }

    public void setProductCategory(ProductCategory productCategory) {
        mProductCategory = productCategory;
    }

    private class ProductCategoryAdapter extends XRecyclerView.Adapter implements IGridLayoutAdapter {

        private static final int PRODUCT_CATEGORY = 0;
        private List<LinkBean> mSubLinks = new ArrayList<>();

        ProductCategoryAdapter() {

        }

        public void setData(List<LinkBean> subs) {
            mSubLinks.clear();
            mSubLinks.addAll(subs);
            notifyDataSetChanged();
        }

        @Override
        public int getItemViewType(int position) {
            return PRODUCT_CATEGORY;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new SubCategoryViewHolder(
                LayoutInflater.from(parent.getContext()).inflate(R.layout.view_sub_category, parent, false)
            );
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof SubCategoryViewHolder) {
                ((SubCategoryViewHolder) holder).render(mSubLinks.get(position));
            }
        }


        @Override
        public int getItemCount() {
            return mSubLinks.size();
        }

        @Override
        public int getSpanSize(int spanCount, int position) {
            return 1;
        }
    }

    class BaseViewHolder extends RecyclerView.ViewHolder {
        public BaseViewHolder(View itemView) {
            super(itemView);
        }
    }

    class SubCategoryViewHolder extends RecyclerView.ViewHolder {
        private FrescoImageView mImage;
        private TextView mLabel;
        private View mRoot;

        public SubCategoryViewHolder(View itemView) {
            super(itemView);
            init(itemView);
        }

        private void init(View v) {
            mImage = v.findViewById(R.id.image);
            mLabel = v.findViewById(R.id.label);
            mRoot = v;
        }

        public void render(final LinkBean link) {
            if (mLabel != null) {
                mLabel.setText(link.label);
            }
            if (mImage != null) {
                mImage.loadView(link.img);
            }
            mRoot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    RouterHelper.toUrl(getActivity(), link.link);
                }
            });
        }

    }

    class SpanSizeLookup extends GridLayoutManager.SpanSizeLookup {

        @Override
        public int getSpanSize(int position) {
            return 3;
        }
    }
}
